package com.laiketui.common.mapper;

import com.laiketui.domain.product.GroupOpenModel;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.db.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 用户拼团
 *
 * @author Trick
 * @date 2020/12/18 15:30
 */
public interface GroupOpenModelMapper extends BaseMapper<GroupOpenModel> {


    /**
     * 获取用户平团信息
     *
     * @param storeId -
     * @param orderno -
     * @return Map
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2020/12/18 15:32
     */
    @Select("SELECT o.id orderId,o.z_price,goo.uid,goo.ptcode,goo.ptgoods_id,goo.endtime,goo.addtime,goo.ptstatus,goo.ptnumber,goo.groupman,goo.platform_activities_id," +
            "goo.activity_no,goo.group_data,goo.group_level,o.z_price,o.offset_balance " +
            "FROM lkt_go_group_order o LEFT JOIN lkt_group_open goo ON o.ptcode = goo.ptcode " +
            " WHERE o.sNo = #{orderno} AND o.store_id = #{storeId}")
    Map<String, Object> getGroupOpen(int storeId, String orderno) throws LaiKeAPIException;


    /**
     * goodscode
     *
     * @param goodsId    -
     * @param activityNo -
     * @return Map
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/3/26 14:53
     */
    @Select("select g.g_status,p.status,g.is_show,g.group_level,g.sales_volume    from lkt_group_product as g " +
            "                    right join lkt_product_list as p on g.product_id=p.id " +
            "where p.id=#{goodsId} and g.is_delete = 0 and   g.activity_no=#{activityNo} limit 1")
    Map<String, Object> getGoodscode(int goodsId, int activityNo) throws LaiKeAPIException;

    /**
     * 获取拼团订单详情信息
     *
     * @param storeId -
     * @param orderId - 拼团订单id
     * @return Map
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/3/24 16:26
     */
    @Select("SELECT k.ptgoods_id, k.ptnumber, k.addtime AS cantime, k.endtime, k.ptstatus , k.groupman, p.NAME, p.num, p.sNo, p.sheng , p.shi, p.xian, p.address, " +
            " p.mobile, p.STATUS , p.id, d.p_name, d.p_price, d.sid " +
            "FROM lkt_group_open k RIGHT JOIN lkt_go_group_order p ON k.ptcode = p.ptcode LEFT JOIN lkt_go_group_order_details d ON p.sNo = d.r_sNo " +
            " WHERE p.store_id = #{storeId} AND p.id = #{orderId}")
    Map<String, Object> getGroupOpenInfoByOid(int storeId, int orderId) throws LaiKeAPIException;


    /**
     * 获取其它拼团信息
     *
     * @param storeId -
     * @param goodsId -
     * @param sysDate - 系统时间
     * @param userId  -
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/3/24 16:48
     */
    @Select("select g.ptcode,g.ptnumber,g.groupman,g.endtime,cast(u.user_name as char) user_name,u.headimgurl,o.sNo from lkt_group_open as g left join lkt_user as u on g.uid=u.user_id " +
            "left join lkt_go_group_order as o on g.ptcode=o.ptcode where g.store_id = #{storeId} and g.ptgoods_id=#{goodsId} and g.ptstatus=1 and  u.store_id = #{storeId}  " +
            "and EXISTS(select * from lkt_go_group_order as o where o.ptcode=g.ptcode and o.status!=6) and g.uid!=#{userId} and o.pid='kaituan' " +
            "and g.endtime>#{sysDate}")
    List<Map<String, Object>> getOterGroupInfo(int storeId, int goodsId, String userId, Date sysDate) throws LaiKeAPIException;


    /**
     * 增加拼团数量
     *
     * @param storeId -
     * @param ptcode  -
     * @return LaiKeAPIException
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/4/1 16:41
     */
    @Update("update lkt_group_open set ptnumber=ptnumber+1 where store_id = #{storeId} and ptcode=#{ptcode}")
    int addPtNum(int storeId, String ptcode) throws LaiKeAPIException;

    /**
     * 根据结束时间获取开团信息
     *
     * @param storeId -
     * @param sysDate -
     * @return List
     * @author Trick
     * @date 2021/4/6 17:06
     */
    @Select("select * from lkt_group_open where store_id = #{storeId} and endtime < #{sysDate} and ptstatus=1")
    List<GroupOpenModel> selectGroupOpenByEndTime(int storeId, Date sysDate);


    /**
     * 查询拼团所有人数
     *
     * @param storeId -
     * @return List
     * @author Trick
     * @date 2021/5/8 16:55
     */
    @Select("SELECT distinct groupman FROM lkt_group_open WHERE store_id = #{storeId} and groupman > 0")
    List<Integer> selectGroupOpenByAll(int storeId);


    /**
     * 修改开团结束时间
     *
     * @param storeId   -
     * @param endtime   -
     * @param ptgoodsId -
     * @return int
     * @author Trick
     * @date 2021/5/10 16:30
     */
    @Update("update lkt_group_open set endtime = #{endtime}  where store_id = #{storeId} and (ptstatus =0 or ptstatus =1)")
    int endGroup(int storeId, String endtime, int ptgoodsId);

}